package com.gc.cscott.verify;

import com.gc.cscott.domain.contest.numbers.TaskNumber;

/**
 *
 * @author root
 */
class JavaOutputVerifier extends AbstractOutputVerifier{
    public VerifiedOutput verifyOuput(TaskNumber taskNumber, String inputText, String outputText, String validationHintText) {
        String location = getVerifierLocation(taskNumber, "Java");
        try {
            Class verifierClass = Class.forName(location);
            try {
                Object verifierInstance = verifierClass.newInstance();
                if (verifierInstance instanceof JavaTaskOutputVerifier==false){
                    throw new Error("Wrong task of verifieyr instance for task: "+taskNumber+" (location: "+location+"). Type "+verifierInstance.getClass().getName()+" expected to implement interface "+JavaTaskOutputVerifier.class.getName());
                } else {
                    JavaTaskOutputVerifier verifier = (JavaTaskOutputVerifier) verifierInstance;
                    try {
                        VerifiedOutput result = verifier.verify(inputText, outputText, validationHintText);
                        return result;
                    } catch(Exception e){
                        throw new Error("Exception raised from verifier for task "+taskNumber, e);
                    }
                }
            } catch (InstantiationException e) {
                throw new Error("Can not instantiate verifier instance for task: "+taskNumber+" (location: "+location+")", e);
            } catch (IllegalAccessException e) {
                throw new Error("Can not instantiate verifier instance for task: "+taskNumber+" (location: "+location+")", e);
            }
        } catch (ClassNotFoundException e) {
            throw new Error("Can not find verifier for task: "+taskNumber+" (location: "+location+")", e);
        }
    }

}
